﻿using System;
using System.Collections.Generic;
using Newtonsoft.Json.Linq;
using System.IO;
using System.Windows.Forms;

namespace Client.Managers
{
    class ZXGManager
    {
        string _path = System.Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + @"\yystock\userconfig\";
        string _fileName = @"zxg.json";
        public List<ZXGModel> zxgList { get; private set; }

        public string zxKind {
            get => _zxkind;
            set => _zxkind = value;
        }
        string _zxkind = "";
        public event System.Action<string[]> removedZXG;
        public event System.Action addedZXG;

        public ZXGManager()
        {
            zxgList = new List<ZXGModel>();
            Load();
        }

        void Load()
        {
            if (File.Exists(_path + _fileName))
            {
                JArray ja = JArray.Parse(System.IO.File.ReadAllText(_path + _fileName));
                foreach (JObject jo in ja)
                {
                    string name = (string)jo["name"];
                    string stocks = (string)jo["stocks"];
                    zxgList.Add(new ZXGModel(name, stocks));
                }
            }
            else
            {
                zxgList.Add(new ZXGModel("自选股", ""));
            }
        }

        void Save()
        {
            if (!Directory.Exists(_path))
                Directory.CreateDirectory(_path);
            JArray a = new JArray();
            foreach (var i in zxgList)
            {
                JObject jo = new JObject();
                jo["name"] = i.name;
                jo["stocks"] = i.stocksString;
                a.Add(jo);
            }
            File.WriteAllText(_path + _fileName, a.ToString());
        }

        public string[] GetKindNames()
        {
            string[] s = new string[zxgList.Count];
            for(int i = 0; i < zxgList.Count; i++)
            {
                s[i] = zxgList[i].name;
            }
            return s;
        }

        public string[] GetSymbols(string kindName)
        {
            foreach(var i in zxgList)
            {
                if (i.name == kindName)
                    return i.stocksArray;
            }
            return null;
        }
        
        public void AddKind(string kindName)
        {
            zxgList.Add(new ZXGModel(kindName, ""));
            Save();
        }


        public void AddZXG(DataCenter.Models.StockInfo stockInfo)
        {
            if (stockInfo != null)
            {
                var a = new Forms.AddZXGForm(stockInfo);
                a.ShowDialog();
            }
        }

        public void AddStock(string symbol)
        {
            if (zxKind == "")
            {
                MessageBox.Show("没有选中默认自选分类");
                return;
            }
            for (int i = 0; i < zxgList.Count; i++)
            {
                if (zxgList[i].name == zxKind)
                {
                    zxgList[i].AddStock(symbol);
                    Save();
                    Managers.Manager.beepM.PlayAddedZXG();
                    return;
                }
            }
        }

        public void RemoveKind(string kindName)
        {
            for(int i = 0; i < zxgList.Count; i++)
            {
                if (zxgList[i].name == kindName)
                {
                    zxgList.RemoveAt(i);
                    Save();
                    return;
                }
            }
        }

        public void removeZXG(DataCenter.Models.StockInfo stockInfo)
        {
            if (stockInfo!=null && zxKind != "")
            {
                if(RemoveStock(zxKind, stockInfo.symbol))
                {
                    removedZXG?.Invoke(GetSymbols(zxKind));
                    MessageBox.Show("删除成功", "提示");
                    return;
                }
                else
                {
                    MessageBox.Show("删除失败。该分类下没有指定的股票信息。", "提示");
                    return;
                }
            }
            MessageBox.Show("删除失败。没有指定分类或没有传入股票信息", "提示");
        }

        public bool RemoveStock(string kindName,string symbol)
        {
            for(int i = 0; i < zxgList.Count; i++)
            {
                if (zxgList[i].name == kindName)
                {
                    if (zxgList[i].RemoveStock(symbol))
                    {
                        Save();
                        return true;
                    }
                    return false;
                }
            }
            return false;
        }

        public bool Exist(string kindName,string symbol)
        {
            if (kindName == "" || kindName == null || symbol == "" || symbol == null)
                return false;

            foreach(var i in zxgList)
            {
                if (i.name == kindName)
                {
                    foreach(string j in i.stocksArray)
                    {
                        if (j == symbol)
                            return true;
                    }
                }
            }

            return false;
        }

        public bool Exist(string kindName)
        {
            foreach(var i in zxgList)
            {
                if (i.name == kindName)
                    return true;
            }

            return false;
        }
    }
}
